
package mapper;

import Utilities.DBConnection;
import dto.DTO_HoaDon;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MAP_HoaDon {

    public boolean themHD(DTO_HoaDon hd)
    {
        boolean flag = false;
        String qry = "insert into HoaDon(NgayLapHD,NgayGiaoHang,DiaChiGiaoHang,MaKH) values('"+hd.getNgayLapHD()+"','"+hd.getNgayGiaoHang()+"','"+hd.getDiachiGiaoHang()+"',"+hd.getMaKH()+")";
        int a = DBConnection.executeQueryString(qry);
        if(a == 1)
        {
            flag = true;
        }
        return flag;
    }
    public int LayMaHD(int MaKH) throws SQLException
    {
        String qry = "select top 1 * from HoaDon where MaKH = "+MaKH +" order by MaHD desc";
        ResultSet rs = DBConnection.getResultSet(qry);
        rs.next();
        int maHD = rs.getInt(1);
        return maHD;

    }
    public ArrayList<DTO_HoaDon> LoadDSHD(int TrangThai)
    {
        ArrayList<DTO_HoaDon> dshd = new ArrayList<DTO_HoaDon>();
        try {
            String qry = "select MaHD,NgayLapHD,NgayGiaoHang,HoTen,DiaChiGiaoHang,TrangThai from HoaDon h,KhachHang k where h.MaKH = k.MaKH and h.TrangThai = " + TrangThai;
            ResultSet rs = DBConnection.getResultSet(qry);
            while (rs != null && rs.next()) {
                DTO_HoaDon hd = new DTO_HoaDon();
                hd.setMaHD(rs.getInt(1));
                hd.setNgayLapHD(rs.getDate(2).toString());
                hd.setNgayGiaoHang(rs.getDate(3).toString());
                hd.setHoTen(rs.getString(4));
                hd.setDiachiGiaoHang(rs.getString(5));
                hd.setTrangThai(rs.getInt(6));

                dshd.add(hd);

            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_HoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dshd;
    }
    public boolean CapNhatHD(DTO_HoaDon hd)
    {
        String qry = "update HoaDon set NgayGiaoHang = '"+hd.getNgayGiaoHang()+"' , DiaChiGiaoHang = '"+hd.getDiachiGiaoHang()+"' , TrangThai = "+hd.getTrangThai()+" where MaHD = "+hd.getMaHD();
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;
    }
    public boolean XoaHD(int MaHD)
    {
        String qry = "delete from HoaDon where MaHD = "+MaHD;
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;
    }

}
